home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / gfx / show / vmpeg.lha / src / mpegaudio / decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-06  |  22.4 KB  |  626 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. decode.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com          *
  10.  *                                                                    *
  11.  * VERSION 3.9                                                       *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  17.  *                                      dfilter to defilter           *
  18.  *                                      dwindow to dewindow           *
  19.  *                              integrated "quantizer", "scalefactor" *
  20.  *                              combined window_samples routine into  *
  21.  *                              filter samples                        *
  22.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  23.  *                              create_syn_filter and introduced a    *
  24.  *                              new Sub-Band Synthesis routine called *
  25.  *                              SubBandSynthesis()                    *
  26.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  27.  *                              Changed "out_fifo()" so that last     *
  28.  *                              unfilled block is also written out.   *
  29.  *                              "create_syn_filter()" was modified so *
  30.  *                              that calculation precision is same as *
  31.  *                              in specification tables.              *
  32.  *                              Changed "decode_scale()" to reflect   *
  33.  *                              specifications.                       *
  34.  *                              Removed all routines used by          *
  35.  *                              "synchronize_buffer()".  This is now  *
  36.  *                              replaced by "seek_sync()".            *
  37.  *                              Incorporated Jean-Georges Fritsch's   *
  38.  *                              "bitstream.c" package.                *
  39.  *                              Deleted "reconstruct_sample()".       *
  40.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  41.  *                              args to out_fifo() - were global.     *
  42.  *                              Moved "alloc_*" reader to common.c.   *
  43.  *                              alloc, sblimit, stereo passed via new *
  44.  *                              'frame_params struct (were globals).  *
  45.  *                              Added JOINT STEREO decoding, lyrs I&II*
  46.  *                              Affects: decode_bitalloc,buffer_samps *
  47.  *                              Plus a few other cleanups.            *
  48.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  49.  *                              II_dequantize_sample to handle range  *
  50.  *                              problems in MSDOS version             *
  51.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  52.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  53.  *           Don H. Lee,                                              *
  54.  *           Peter W. Farrett                                         *
  55.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  56.  *                              newly introduced functions are        *
  57.  *                              buffer_CRC and recover_CRC_error.     *
  58.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  59.  *                              important fixes involved changing     *
  60.  *                              16-bit ints to long or unsigned in    *
  61.  *                              bit alloc routines for quant of 65535 *
  62.  *                              and passing proper function args.     *
  63.  *                              Removed "Other Joint Stereo" option   *
  64.  *                              and made bitrate be total channel     *
  65.  *                              bitrate, irrespective of the mode.    *
  66.  *                              Fixed many small bugs & reorganized.  *
  67.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  68.  **********************************************************************/
  69.  
  70. #include        "common.h"
  71. #include        "decoder.h"
  72. #include  "tables/dewindow.h"
  73.  
  74. /***************************************************************
  75. /*
  76. /* This module contains the core of the decoder ie all the
  77. /* computational routines. (Layer I and II only)
  78. /* Functions are common to both layer unless
  79. /* otherwise specified.
  80. /*
  81. /***************************************************************/
  82.  
  83. /*****************************************************************
  84. /*
  85. /* The following routines decode the system information
  86. /*
  87. /****************************************************************/
  88.  
  89. /************ Layer I, Layer II & Layer III ******************/
  90.  
  91. #ifndef __PPC__
  92. void decode_info(bs, fr_ps)
  93. Bit_stream_struc *bs;
  94. frame_params *fr_ps;
  95. {
  96.     layer *hdr = fr_ps->header;
  97.  
  98.     hdr->version = get1bit(bs);
  99.     hdr->lay = 4-getbits(bs,2);
  100.     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
  101.     hdr->bitrate_index = getbits(bs,4);
  102.     hdr->sampling_frequency = getbits(bs,2);
  103.     hdr->padding = get1bit(bs);
  104.     hdr->extension = get1bit(bs);
  105.     hdr->mode = getbits(bs,2);
  106.     hdr->mode_ext = getbits(bs,2);
  107.     hdr->copyright = get1bit(bs);
  108.     hdr->original = get1bit(bs);
  109.     hdr->emphasis = getbits(bs,2);
  110. }
  111. #endif
  112.  
  113. /*******************************************************************
  114. /*
  115. /* The bit allocation information is decoded. Layer I
  116. /* has 4 bit per subband whereas Layer II is Ws and bit rate
  117. /* dependent.
  118. /*
  119. /********************************************************************/
  120.  
  121. /**************************** Layer II *************/
  122.  
  123. #ifndef __PPC__
  124. void II_decode_bitalloc(bs, bit_alloc, fr_ps)
  125. Bit_stream_struc *bs;
  126. unsigned int bit_alloc[2][SBLIMIT];
  127. frame_params *fr_ps;
  128. {
  129.     int i,j;
  130.     int stereo = fr_ps->stereo;
  131.     int sblimit = fr_ps->sblimit;
  132.     int jsbound = fr_ps->jsbound;
  133.     al_table *alloc = fr_ps->alloc;
  134.  
  135.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  136.         bit_alloc[j][i] = (char) getbits(bs,(*alloc)[i][0].bits);
  137.  
  138.     for (i=jsbound;i<sblimit;i++) /* expand to 2 channels */
  139.         bit_alloc[0][i] = bit_alloc[1][i] =
  140.             (char) getbits(bs,(*alloc)[i][0].bits);
  141.  
  142.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  143.         bit_alloc[j][i] = 0;
  144. }
  145. #endif
  146.  
  147. /**************************** Layer I *************/
  148.  
  149. #ifndef __PPC__
  150. void I_decode_bitalloc(bs, bit_alloc, fr_ps)
  151. Bit_stream_struc *bs;
  152. unsigned int bit_alloc[2][SBLIMIT];
  153. frame_params *fr_ps;
  154. {
  155.     int i,j;
  156.     int stereo  = fr_ps->stereo;
  157.     int sblimit = fr_ps->sblimit;
  158.     int jsbound = fr_ps->jsbound;
  159.     int b;
  160.  
  161.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  162.         bit_alloc[j][i] = getbits(bs,4);
  163.     for (i=jsbound;i<SBLIMIT;i++) {
  164.         b = getbits(bs,4);
  165.         for (j=0;j<stereo;j++)
  166.             bit_alloc[j][i] = b;
  167.     }
  168. }
  169. #endif
  170.  
  171. /*****************************************************************
  172. /*
  173. /* The following two functions implement the layer I and II
  174. /* format of scale factor extraction. Layer I involves reading
  175. /* 6 bit per subband as scale factor. Layer II requires reading
  176. /* first the scfsi which in turn indicate the number of scale factors
  177. /* transmitted.
  178. /*    Layer I : I_decode_scale
  179. /*   Layer II : II_decode_scale
  180. /*
  181. /****************************************************************/
  182.  
  183. /************************** Layer I stuff ************************/
  184.  
  185. void I_decode_scale(bs, bit_alloc, scale_index, fr_ps)
  186. Bit_stream_struc *bs;
  187. unsigned int bit_alloc[2][SBLIMIT], scale_index[2][3][SBLIMIT];
  188. frame_params *fr_ps;
  189. {
  190.     int i,j;
  191.     int stereo = fr_ps->stereo;
  192.     int sblimit = fr_ps->sblimit;
  193.  
  194.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  195.         if (!bit_alloc[j][i])
  196.             scale_index[j][0][i] = SCALE_RANGE-1;
  197.         else                    /* 6 bit per scale factor */
  198.             scale_index[j][0][i] =  getbits(bs,6);
  199.  
  200. }
  201.  
  202. /*************************** Layer II stuff ***************************/
  203.  
  204. void II_decode_scale(bs,scfsi, bit_alloc,scale_index, fr_ps)
  205. Bit_stream_struc *bs;
  206. unsigned int scfsi[2][SBLIMIT], bit_alloc[2][SBLIMIT],
  207.              scale_index[2][3][SBLIMIT];
  208. frame_params *fr_ps;
  209. {
  210.     int i,j;
  211.     int stereo = fr_ps->stereo;
  212.     int sblimit = fr_ps->sblimit;
  213.    
  214.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) /* 2 bit scfsi */
  215.         if (bit_alloc[j][i]) scfsi[j][i] = (char) getbits(bs,2);
  216.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)   
  217.         scfsi[j][i] = 0;
  218.  
  219.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  220.         if (bit_alloc[j][i])   
  221.             switch (scfsi[j][i]) {
  222.                 /* all three scale factors transmitted */
  223.              case 0 : scale_index[j][0][i] = getbits(bs,6);
  224.                 scale_index[j][1][i] = getbits(bs,6);
  225.                 scale_index[j][2][i] = getbits(bs,6);
  226.                 break;
  227.                 /* scale factor 1 & 3 transmitted */
  228.              case 1 : scale_index[j][0][i] =
  229.                  scale_index[j][1][i] = getbits(bs,6);
  230.                 scale_index[j][2][i] = getbits(bs,6);
  231.                 break;
  232.                 /* scale factor 1 & 2 transmitted */
  233.              case 3 : scale_index[j][0][i] = getbits(bs,6);
  234.                 scale_index[j][1][i] =
  235.                     scale_index[j][2][i] =  getbits(bs,6);
  236.                 break;
  237.                 /* only one scale factor transmitted */
  238.              case 2 : scale_index[j][0][i] =
  239.                  scale_index[j][1][i] =
  240.                      scale_index[j][2][i] = getbits(bs,6);
  241.                 break;
  242.                 default : break;
  243.             }
  244.         else {
  245.             scale_index[j][0][i] = scale_index[j][1][i] =
  246.                 scale_index[j][2][i] = SCALE_RANGE-1;
  247.         }
  248.     }
  249.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {
  250.         scale_index[j][0][i] = scale_index[j][1][i] =
  251.             scale_index[j][2][i] = SCALE_RANGE-1;
  252.     }
  253. }
  254.  
  255. /**************************************************************
  256. /*
  257. /*   The following two routines take care of reading the
  258. /* compressed sample from the bit stream for both layer 1 and
  259. /* layer 2. For layer 1, read the number of bits as indicated
  260. /* by the bit_alloc information. For layer 2, if grouping is
  261. /* indicated for a particular subband, then the sample size has
  262. /* to be read from the bits_group and the merged samples has
  263. /* to be decompose into the three distinct samples. Otherwise,
  264. /* it is the same for as layer one.
  265. /*
  266. /**************************************************************/
  267.  
  268. /******************************* Layer I stuff ******************/
  269.  
  270. #ifndef __PPC__
  271. void I_buffer_sample(bs, sample, bit_alloc, fr_ps)
  272. unsigned int FAR sample[2][3][SBLIMIT];
  273. unsigned int bit_alloc[2][SBLIMIT];
  274. Bit_stream_struc *bs;
  275. frame_params *fr_ps;
  276. {
  277.     int i,j,k;
  278.     int stereo = fr_ps->stereo;
  279.     int sblimit = fr_ps->sblimit;
  280.     int jsbound = fr_ps->jsbound;
  281.     unsigned int s;
  282.  
  283.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  284.         if ( (k = bit_alloc[j][i]) == 0)
  285.             sample[j][0][i] = 0;
  286.         else 
  287.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  288.     for (i=jsbound;i<SBLIMIT;i++) {
  289.         if ( (k = bit_alloc[0][i]) == 0)
  290.             s = 0;
  291.         else 
  292.             s = (unsigned int)getbits(bs,k+1);
  293.         for (j=0;j<stereo;j++)
  294.             sample[j][0][i]    = s;
  295.     }
  296. }
  297. #endif
  298.  
  299. /*************************** Layer II stuff ************************/
  300.  
  301. #ifndef __PPC__
  302. void II_buffer_sample(bs,sample,bit_alloc,fr_ps)
  303. unsigned int FAR sample[2][3][SBLIMIT];
  304. unsigned int bit_alloc[2][SBLIMIT];
  305. Bit_stream_struc *bs;
  306. frame_params *fr_ps;
  307. {
  308.     int i,j,k,m;
  309.     int stereo = fr_ps->stereo;
  310.     int sblimit = fr_ps->sblimit;
  311.     int jsbound = fr_ps->jsbound;
  312.     al_table *alloc = fr_ps->alloc;
  313.  
  314.     for (i=0;i<sblimit;i++) for (j=0;j<((i<jsbound)?stereo:1);j++) {
  315.         if (bit_alloc[j][i]) {
  316.             /* check for grouping in subband */
  317.             if ((*alloc)[i][bit_alloc[j][i]].group==3)
  318.                 for (m=0;m<3;m++) {
  319.                     k = (*alloc)[i][bit_alloc[j][i]].bits;
  320.                     sample[j][m][i] = (unsigned int) getbits(bs,k);
  321.                 }         
  322.             else {              /* bit_alloc = 3, 5, 9 */
  323.                 unsigned int nlevels, c=0;
  324.  
  325.                 nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  326.                 k=(*alloc)[i][bit_alloc[j][i]].bits;
  327.                 c = (unsigned int) getbits(bs, k);
  328.                 for (k=0;k<3;k++) {
  329.                     sample[j][k][i] = c % nlevels;
  330.                     c /= nlevels;
  331.                 }
  332.             }
  333.         }
  334.         else {                  /* for no sample transmitted */
  335.             for (k=0;k<3;k++) sample[j][k][i] = 0;
  336.         }
  337.         if(stereo == 2 && i>= jsbound) /* joint stereo : copy L to R */
  338.             for (k=0;k<3;k++) sample[1][k][i] = sample[0][k][i];
  339.     }
  340.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) for (k=0;k<3;k++)
  341.         sample[j][k][i] = 0;
  342. }      
  343. #endif
  344.  
  345. /**************************************************************
  346. /*
  347. /*   Restore the compressed sample to a factional number.
  348. /*   first complement the MSB of the sample
  349. /*    for layer I :
  350. /*    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  351. /*   for Layer II :
  352. /*   Use the formula s = s' * c + d
  353. /*
  354. /**************************************************************/
  355.  
  356. static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
  357.                         1.77777777777, 1.06666666666, 1.03225806452,
  358.                         1.01587301587, 1.00787401575, 1.00392156863,
  359.                         1.00195694716, 1.00097751711, 1.00048851979,
  360.                         1.00024420024, 1.00012208522, 1.00006103888,
  361.                         1.00003051851, 1.00001525902 };
  362.  
  363. static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
  364.                         0.125000000, 0.062500000, 0.031250000, 0.015625000,
  365.                         0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  366.                         0.00048828125, 0.00024414063, 0.00012207031,
  367.                         0.00006103516, 0.00003051758 };
  368.  
  369. /************************** Layer II stuff ************************/
  370.  
  371. void II_dequantize_sample(sample, bit_alloc, fraction, fr_ps)
  372. unsigned int FAR sample[2][3][SBLIMIT];
  373. unsigned int bit_alloc[2][SBLIMIT];
  374. double FAR fraction[2][3][SBLIMIT];
  375. frame_params *fr_ps;
  376. {
  377.     int i, j, k, x;
  378.     int stereo = fr_ps->stereo;
  379.     int sblimit = fr_ps->sblimit;
  380.     al_table *alloc = fr_ps->alloc;
  381.  
  382.     for (i=0;i<sblimit;i++)  for (j=0;j<3;j++) for (k=0;k<stereo;k++)
  383.         if (bit_alloc[k][i]) {
  384.  
  385.             /* locate MSB in the sample */
  386.             x = 0;
  387. #ifndef MS_DOS
  388.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps) x++;
  389. #else
  390.             /* microsoft C thinks an int is a short */
  391.             while (( (unsigned long) (1L<<(long)x) <
  392.                     (unsigned long)( (*alloc)[i][bit_alloc[k][i]].steps)
  393.                     ) && ( x < 16) ) x++;
  394. #endif
  395.  
  396.             /* MSB inversion */
  397.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  398.                 fraction[k][j][i] = 0.0;
  399.             else  fraction[k][j][i] = -1.0;
  400.  
  401.             /* Form a 2's complement sample */
  402.             fraction[k][j][i] += (double) (sample[k][j][i] & ((1<<x-1)-1)) /
  403.                 (double) (1L<<x-1);
  404.  
  405.             /* Dequantize the sample */
  406.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  407.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  408.         }
  409.         else fraction[k][j][i] = 0.0;   
  410.    
  411.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<3;j++) for(k=0;k<stereo;k++)
  412.         fraction[k][j][i] = 0.0;
  413. }
  414.  
  415. /***************************** Layer I stuff ***********************/
  416.  
  417. void I_dequantize_sample(sample, fraction, bit_alloc, fr_ps)
  418. unsigned int FAR sample[2][3][SBLIMIT];
  419. unsigned int bit_alloc[2][SBLIMIT];
  420. double FAR fraction[2][3][SBLIMIT];
  421. frame_params *fr_ps;
  422. {
  423.     int i, nb, k;
  424.     int stereo = fr_ps->stereo;
  425.     int sblimit = fr_ps->sblimit;
  426.  
  427.     for (i=0;i<SBLIMIT;i++)
  428.         for (k=0;k<stereo;k++)
  429.             if (bit_alloc[k][i]) {
  430.                 nb = bit_alloc[k][i] + 1;
  431.                 if (((sample[k][0][i] >> nb-1) & 1) == 1) fraction[k][0][i] = 0.0;
  432.                 else fraction[k][0][i] = -1.0;
  433.                 fraction[k][0][i] += (double) (sample[k][0][i] & ((1<<nb-1)-1)) /
  434.                     (double) (1L<<nb-1);
  435.  
  436.                 fraction[k][0][i] =
  437.                     (double) (fraction[k][0][i]+1.0/(double)(1L<<nb-1)) *
  438.                         (double) (1L<<nb) / (double) ((1L<<nb)-1);
  439.             }
  440.             else fraction[k][0][i] = 0.0;
  441. }
  442.  
  443. /************************************************************
  444. /*
  445. /*   Restore the original value of the sample ie multiply
  446. /*    the fraction value by its scalefactor.
  447. /*
  448. /************************************************************/
  449.  
  450. /************************* Layer II Stuff **********************/
  451.  
  452. void II_denormalize_sample(fraction, scale_index,fr_ps,x)
  453. double FAR fraction[2][3][SBLIMIT];
  454. unsigned int scale_index[2][3][SBLIMIT];
  455. frame_params *fr_ps;
  456. int x;
  457. {
  458.     int i,j,k;
  459.     int stereo = fr_ps->stereo;
  460.     int sblimit = fr_ps->sblimit;
  461.  
  462.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  463.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  464.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  465.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  466.     }
  467. }
  468.  
  469. /**************************** Layer I stuff ******************************/
  470.  
  471. void I_denormalize_sample(fraction,scale_index,fr_ps)
  472. double FAR fraction[2][3][SBLIMIT];
  473. unsigned int scale_index[2][3][SBLIMIT];
  474. frame_params *fr_ps;
  475. {
  476.     int i,j,k;
  477.     int stereo = fr_ps->stereo;
  478.     int sblimit = fr_ps->sblimit;
  479.  
  480.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  481.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  482. }
  483.  
  484. /*****************************************************************
  485. /*
  486. /* The following are the subband synthesis routines. They apply
  487. /* to both layer I and layer II stereo or mono. The user has to
  488. /* decide what parameters are to be passed to the routines.
  489. /*
  490. /***************************************************************/
  491.  
  492. /*************************************************************
  493. /*
  494. /*   Pass the subband sample through the synthesis window
  495. /*
  496. /**************************************************************/
  497.  
  498. /* create in synthesis filter */
  499.  
  500. void create_syn_filter(filter)
  501. double FAR filter[64][SBLIMIT];
  502. {
  503.     register int i,k;
  504.  
  505.     for (i=0; i<64; i++)
  506.         for (k=0; k<32; k++) {
  507.             if ((filter[i][k] = 1e9*cos((double)((PI64*i+PI4)*(2*k+1)))) >= 0)
  508.                 modf(filter[i][k]+0.5, &filter[i][k]);
  509.             else
  510.                 modf(filter[i][k]-0.5, &filter[i][k]);
  511.             filter[i][k] *= 1e-9;
  512.         }
  513. }
  514.  
  515. /***************************************************************
  516. /*
  517. /*   Window the restored sample
  518. /*
  519. /***************************************************************/
  520.  
  521. /* read in synthesis window */
  522.  
  523. int SubBandSynthesis (bandPtr, channel, samples)
  524. double *bandPtr;
  525. int channel;
  526. short *samples;
  527. {
  528.     register int i,j,k;
  529.     register double *bufOffsetPtr, sum;
  530.     static int init = 1;
  531.     typedef double NN[64][32];
  532.     static NN FAR *filter;
  533.     typedef double BB[2][2*HAN_SIZE];
  534.     static BB FAR *buf;
  535.     static int bufOffset = 64;
  536.     static double FAR *window;
  537.     int clip = 0;               /* count & return how many samples clipped */
  538.  
  539.     if (init) {
  540.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  541.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  542.         create_syn_filter(*filter);
  543.         window = (double FAR *)&t_window;
  544.         bufOffset = 64;
  545.         init = 0;
  546.     }
  547.     if (channel == 0) bufOffset = (bufOffset - 64) & 0x3ff;
  548.     bufOffsetPtr = &((*buf)[channel][bufOffset]);
  549.  
  550.     for (i=0; i<64; i++) {
  551.         sum = 0;
  552.         for (k=0; k<32; k++)
  553.             sum += bandPtr[k] * (*filter)[i][k];
  554.         bufOffsetPtr[i] = sum;
  555.     }
  556.     /*  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)  */
  557.     /*  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)  */
  558.     for (j=0; j<32; j++) {
  559.         sum = 0;
  560.         for (i=0; i<16; i++) {
  561.             k = j + (i<<5);
  562.             sum += window[k] * (*buf) [channel] [( (k + ( ((i+1)>>1) <<6) ) +
  563.                                                   bufOffset) & 0x3ff];
  564.         }
  565.  
  566. /*   {long foo = (sum > 0) ? sum * SCALE + 0.5 : sum * SCALE - 0.5; */
  567.      {long foo = sum * SCALE;
  568.      if (foo >= (long) SCALE)      {samples[j] = SCALE-1; ++clip;}
  569.      else if (foo < (long) -SCALE) {samples[j] = -SCALE;  ++clip;}
  570.      else                           samples[j] = foo;
  571.  }
  572.     }
  573.     return(clip);
  574. }
  575.  
  576. void  buffer_CRC(bs, old_crc)
  577. Bit_stream_struc  *bs;
  578. unsigned int  *old_crc;
  579. {
  580.     *old_crc = getbits(bs, 16);
  581. }
  582.  
  583. void  recover_CRC_error(pcm_sample, error_count, fr_ps, outFile, psampFrames)
  584. short FAR pcm_sample[2][3][SBLIMIT];
  585. int error_count;
  586. frame_params *fr_ps;
  587. FILE *outFile;
  588. unsigned long *psampFrames;
  589. {
  590.     int  stereo = fr_ps->stereo;
  591.     int  num, done, i;
  592.     int  samplesPerFrame, samplesPerSlot;
  593.     layer  *hdr = fr_ps->header;
  594.     long  offset;
  595.     short  *temp;
  596.  
  597.     num = 3;
  598.     if (hdr->lay == 1) num = 1;
  599.  
  600.     samplesPerSlot = SBLIMIT * num * stereo;
  601.     samplesPerFrame = samplesPerSlot * 32;
  602.  
  603.     if (error_count == 1) {     /* replicate previous error_free frame */
  604.         done = 1;
  605.         /* flush out fifo */
  606.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  607.         /* go back to the beginning of the previous frame */
  608.         offset = sizeof(short int) * samplesPerFrame;
  609.         fseek(outFile, -offset, SEEK_CUR);
  610.         done = 0;
  611.         for (i = 0; i < SCALE_BLOCK; i++) {
  612.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  613.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  614.         }
  615.     }
  616.     else{                       /* mute the frame */
  617.         temp = (short*) pcm_sample;
  618.         done = 0;
  619.         for (i = 0; i < 2*3*SBLIMIT; i++)
  620.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  621.         for (i = 0; i < SCALE_BLOCK; i++)
  622.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  623.     }
  624. }
  625.  
  626.